// quadratic.cpp
//
// This program solves a quadratic equation in standard form.
// The program prompts the user for three values -- A, B, and C,
// which correspond to the coefficients of a quadratic equation
// in standard form:
//
// Ax^2 + Bx + C = 0
//
// Console-based
// Retruns real as well as comples roots.
//
// copyright: Jose M. Blanco, 2002#include <stdio.h>
#include <iostream.h>
#include <math.h>
// function to evaluate the determinantdouble determinant(double arg_a, double arg_b, double arg_c){return(arg_b * arg_b) - (4 * arg_a * arg_c);
}// end determinant// function to evaluate real rootsvoid realRoots(double arg_a, double arg_b, double arg_sqrt){double firstRoot = (-arg_b/(2 * arg_a)) +
(arg_sqrt/(2 * arg_a));
double secondRoot = (-arg_b/(2 * arg_a)) -
(arg_sqrt/(2 * arg_a));
cout << "\nFirst Real Root: \t" << firstRoot << "\n";
cout << "Second Real Root: \t" << secondRoot << "\n";
}// end realRootsvoid output1(double first, double second){
cout << "\nFirst Imaginary Root: \t" << first << " + " << second << "i" << "\n";
cout << "\nSecond Imaginary Root: \t" << first << " - " << second << "i" << "\n";
}// end output1()void output2(double first, double second){
second = -(second); // change the sign of the second term, for correct output
cout << "\nFirst Imaginary Root: \t" << first << " - " << second << "i" << "\n";
cout << "\nSecond Imaginary Root: \t" << first << " + " << second << "i" << "\n";
}// end output2()// evaluate imaginary rootsvoid imaginaryRoots(double arg_a, double arg_b, double arg_imag_sqrt){double two_a = 2 * arg_a;
double first_term = (-arg_b)/two_a;
double second_term = (arg_imag_sqrt)/two_a;
if(second_term >= 0){
//cout << "\nsecond_term is greater than or eaqual to 0: " << second_term << "\n";
output1(first_term, second_term);
}// end ifelse{
//cout << "\nsecond_term is less than or eaqual to 0: " << second_term << "\n";
output2(first_term, second_term);
}// end else}// end imaginaryRoots// main functionint main(){
cout << "\n***********************************************************************\n";
cout << "* Find the roots of a quadratic equation in standard form. *";
cout << "\n***********************************************************************\n";
// declare variables a, b, and cdouble a, b, c;
char n;
do{
cout << "\nEnter the value of 'a': ";
cin >> a;
cout << "Enter the value of 'b': ";
cin >> b;
cout << "Enter the value of 'c': ";
cin >> c;
double det = determinant(a, b, c);
if(det >= 0){
realRoots(a, b, sqrt(det)); // roots are real}// end ifelse{
imaginaryRoots(a, b, sqrt(-det)); // roots are imaginary}// end else
cout << "\nWould you like to solve another?";
cout << "\nEnter 'y' for 'Yes' -- 'n' for 'No': ";
cin >> n;
cout << "\n";
}while(n == 'y' || n == 'Y'); // end do-while loopreturn 0;
}